# coding: UTF-8
# Filename : chain_part_api.py
# Company : imedia
# Author : rose
# -*-coding:utf-8 -*-
from api.common.common import *
from api.common import mysql,log,utils,httputils,const
#import mysql,log,utils,httputils,const
import chain_city_data_model as template



def chain_part_city_list(request):
     #记录用户脚印(必做动作)
    httputils.footprint(request)
    #获取请求参数
    cus_id = httputils.post(request, 'cus_id')
    if not utils.is_num(cus_id): cus_id = 1
    #通信验证
    if validate(request):
        return validate(request)
    #中间模板段
    output = ''
    output += template.imedia_start 
    output += get_body_str(cus_id)
    output += template.imedia_end
    #初始化xml返回
    return response(output)

def get_body_str(cus_id):
    sql = '''
    SELECT city_id,b.name 
    FROM chain_part  as a , im_city as b 
    where city_id <> 0 and city_id = b.id and cus_id = %s and active=1 group by city_id
    order by b.up asc 
    '''
    param = [cus_id]
    body_str = ''
    rest = mysql.doSearch(sql, param)
    for obj in rest:
        body_template = template.imedia_body
        body_template = body_template.replace('{id}', str(obj[0]))
        body_template = body_template.replace('{city}', str(obj[1]))
        body_str += body_template
    return body_str

